home *** CD-ROM | disk | FTP | other *** search
- /************************************************************/
- /* */
- /* MDEF Code from Chapter Three of */
- /* */
- /* *** The Macintosh Programming Primer *** */
- /* */
- /* Copyright 1990, Dave Mark */
- /* */
- /* This program demonstrates specific Mac programming */
- /* techniques. */
- /* */
- /************************************************************/
-
- #define MARGIN 2
-
-
- /************************************************************** main ***/
-
- pascal void main( message, theMenu, menuRectPtr, hitPt, whichItemPtr )
- int message;
- MenuHandle theMenu;
- Rect *menuRectPtr;
- Point hitPt;
- int *whichItemPtr;
- {
- short PICTResID, numPicts, maxH, maxV, i;
- PicHandle myPicture;
- Rect r, tempRect;
- int newItem;
-
- switch( message )
- {
- case mDrawMsg:
- GetNumPicts( theMenu, &PICTResID, &numPicts );
- CalcMaxHV( PICTResID, numPicts, &maxH, &maxV );
-
- r.top = menuRectPtr->top + MARGIN/2;
- r.left = menuRectPtr->left + MARGIN;
- r.bottom = r.top + maxV;
- r.right = r.left + maxH;
-
- for ( i=0; i<numPicts; i++ )
- {
- myPicture = GetPicture( PICTResID + i );
- tempRect = r;
- CenterPict( myPicture, &tempRect );
- DrawPicture( myPicture, &tempRect );
- OffsetRect( &r, 0, maxV + MARGIN );
- }
- break;
- case mChooseMsg:
- GetNumPicts( theMenu, &PICTResID, &numPicts );
- CalcMaxHV( PICTResID, numPicts, &maxH, &maxV );
-
- if ( PtInRect( hitPt, menuRectPtr ) )
- {
- newItem = ( (hitPt.v - menuRectPtr->top) / (maxV + MARGIN) ) + 1;
- if ( ( *whichItemPtr > 0 ) && ( *whichItemPtr != newItem ) )
- {
- r = *menuRectPtr;
- r.top += ( (*whichItemPtr-1) * (MARGIN + maxV) );
- r.bottom = r.top + maxV + MARGIN;
- InvertRect( &r );
- }
-
- if ( *whichItemPtr != newItem )
- {
- *whichItemPtr = newItem;
- r = *menuRectPtr;
- r.top += ( (*whichItemPtr-1) * (MARGIN + maxV) );
- r.bottom = r.top + maxV + MARGIN;
- InvertRect( &r );
- }
- }
- else if ( *whichItemPtr > 0 )
- {
- r = *menuRectPtr;
- r.top += ( (*whichItemPtr-1) * (MARGIN + maxV) );
- r.bottom = r.top + maxV + MARGIN;
- InvertRect( &r );
- *whichItemPtr = 0;
- }
- break;
- case mSizeMsg:
- GetNumPicts( theMenu, &PICTResID, &numPicts );
- CalcMaxHV( PICTResID, numPicts, &maxH, &maxV );
- (**theMenu).menuWidth = maxH + 2 * MARGIN;
- (**theMenu).menuHeight = (maxV + MARGIN) * numPicts;
- break;
- }
- }
-
-
- /******************************** CenterPict *********/
-
- CenterPict( thePicture, myRectPtr )
- PicHandle thePicture;
- Rect *myRectPtr;
- {
- Rect windRect, pictureRect;
-
- windRect = *myRectPtr;
- pictureRect = (**( thePicture )).picFrame;
- myRectPtr->top = (windRect.bottom - windRect.top - (pictureRect.bottom - pictureRect.top))
- / 2 + windRect.top;
- myRectPtr->bottom = myRectPtr->top + (pictureRect.bottom - pictureRect.top);
- myRectPtr->left = (windRect.right - windRect.left - (pictureRect.right - pictureRect.left))
- / 2 + windRect.left;
- myRectPtr->right = myRectPtr->left + (pictureRect.right - pictureRect.left);
- }
-
-
- /************************************************************** CalcMaxHV ***/
-
- CalcMaxHV( PICTResID, numPicts, hPtr, vPtr )
- short PICTResID, numPicts, *hPtr, *vPtr;
- {
- short i;
- Rect r;
- PicHandle myPicture;
-
- *hPtr = 0;
- *vPtr = 0;
- for ( i=0; i<numPicts; i++ )
- {
- myPicture = GetPicture( PICTResID + i );
- r = (**myPicture).picFrame;
-
- if ( r.bottom - r.top > *vPtr )
- *vPtr = r.bottom - r.top;
- if ( r.right - r.left > *hPtr )
- *hPtr = r.right - r.left;
- }
- }
-
-
- /************************************************************** GetNumPicts ***/
-
- GetNumPicts( theMenu, baseIDPtr, numPictsPtr )
- MenuHandle theMenu;
- short *baseIDPtr, *numPictsPtr;
- {
- *baseIDPtr = HiWord((**theMenu).enableFlags);
- *numPictsPtr = LoWord((**theMenu).enableFlags);
- }